package Main;

import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener {
	private String s1 = "";
	private String s2 = "";
	private int n1;
	private int n2;
	private int ejeY;
	private int ejeX;
	private JLabel lventana;
	private JPanel p;
	private int res;

	public Main() {
		this.ventana();
		this.inicializar();
	}

	private void inicializar() {
		JButton boton1 = new JButton("/");
		boton1.setActionCommand("dividir");
		boton1.addActionListener(this);

		JButton boton2 = new JButton("*");
		boton2.setActionCommand("multiplicar");
		boton2.addActionListener(this);

		JButton boton3 = new JButton("-");
		boton3.setActionCommand("restar");
		boton3.addActionListener(this);

		JButton boton4 = new JButton("+");
		boton4.setActionCommand("sumar");
		boton4.addActionListener(this);

		JButton botonNum7 = new JButton("7");
		botonNum7.setActionCommand("7");
		botonNum7.addActionListener(this);

		JButton botonNum8 = new JButton("8");
		botonNum8.setActionCommand("8");
		botonNum8.addActionListener(this);

		JButton botonNum9 = new JButton("9");
		botonNum9.setActionCommand("9");
		botonNum9.addActionListener(this);

		JButton botonNum4 = new JButton("4");
		botonNum4.setActionCommand("4");
		botonNum4.addActionListener(this);

		JButton botonNum5 = new JButton("5");
		botonNum5.setActionCommand("5");
		botonNum5.addActionListener(this);

		JButton botonNum6 = new JButton("6");
		botonNum6.setActionCommand("6");
		botonNum6.addActionListener(this);

		JButton botonNum1 = new JButton("1");
		botonNum1.setActionCommand("1");
		botonNum1.addActionListener(this);

		JButton botonNum2 = new JButton("2");
		botonNum2.setActionCommand("2");
		botonNum2.addActionListener(this);

		JButton botonNum3 = new JButton("3");
		botonNum3.setActionCommand("3");
		botonNum3.addActionListener(this);

		JButton botonNum0 = new JButton("0");
		botonNum0.setActionCommand("0");
		botonNum0.addActionListener(this);

		JButton botonEnter = new JButton("=");
		botonEnter.setActionCommand("=");
		botonEnter.addActionListener(this);

		JButton botonC = new JButton("C");
		botonC.setActionCommand("C");
		botonC.addActionListener(this);

		p.setLayout(new GridLayout(5, 1));

		lventana = new JLabel();

		p.add(lventana);

		JPanel panel = new JPanel(new GridLayout(4, 1));
		JPanel panelA = new JPanel(new GridLayout(1, 4));
		JPanel panelB = new JPanel(new GridLayout(1, 4));
		JPanel panelC = new JPanel(new GridLayout(1, 4));
		JPanel panelD = new JPanel(new GridLayout(1, 4));

		p.add(panelA);
		p.add(panelB);
		p.add(panelC);
		p.add(panelD);

		panelA.add(botonNum7);
		panelA.add(botonNum8);
		panelA.add(botonNum9);
		panelA.add(boton1);

		panelB.add(botonNum4);
		panelB.add(botonNum5);
		panelB.add(botonNum6);
		panelB.add(boton2);

		panelC.add(botonNum1);
		panelC.add(botonNum2);
		panelC.add(botonNum3);
		panelC.add(boton3);

		panelD.add(botonC);
		panelD.add(botonNum0);
		panelD.add(botonEnter);
		panelD.add(boton4);

		setVisible(true);
	}

	private void ventana() {
		p = new JPanel();
		int alto = 500;
		int ancho = 400;
		this.ejeX = (Toolkit.getDefaultToolkit().getScreenSize().width / 2)
				- (ancho / 2);
		this.ejeY = (Toolkit.getDefaultToolkit().getScreenSize().height / 2)
				- (alto / 2);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(ancho, alto);
		setLocation(ejeX, ejeY);
		setTitle("Calculadora");
		add(p);

	}

	public static void main(String[] args) {
		new Main();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		switch (e.getActionCommand()) {
		case "1":
			this.s1 = s1 + 1;

			lventana.setText(this.s1);

			break;
		case "2":
			this.s1 = s1 + 2;

			lventana.setText(this.s1);

			break;
		case "3":
			this.s1 = s1 + 3;

			lventana.setText(this.s1);

			break;
		case "4":
			this.s1 = s1 + 4;

			lventana.setText(this.s1);

			break;
		case "5":
			this.s1 = s1 + 5;

			lventana.setText(this.s1);

			break;
		case "6":
			this.s1 = s1 + 6;

			lventana.setText(this.s1);

			break;
		case "7":
			this.s1 = s1 + 7;

			lventana.setText(this.s1);

			break;
		case "8":
			this.s1 = s1 + 8;

			lventana.setText(this.s1);

			break;
		case "9":
			this.s1 = s1 + 9;

			lventana.setText(this.s1);

			break;
		case "dividir":
			;
			break;
		case "0":
			this.s1 = s1 + 0;

			lventana.setText(this.s1);

			break;
		case "multiplicar":
			;
			break;
		case "restar":
			;
			break;
		case "sumar":
			lventana.setText("");
			this.sumar(e);
			break;
		case "C":
			this.borrar(this.n1);
			break;
		}

	}

	public void borrar(int n1) {
		this.n1 = n1 % 10;

	}

	private void sumar(ActionEvent e) {
		this.n1 = Integer.parseInt(s1);
		this.s1 = "";
		switch (e.getActionCommand()) {
		case "1":
			this.n2 = n2 + 1;
			lventana.setText(this.n1 + "+" + this.s2);
			lventana.getText();
			break;
		case "2":
			this.s2 = s2 + 2;
			String.valueOf(n2);
			lventana.setText(this.s2);
			break;
		case "3":
			this.s2 = s2 + 3;
			lventana.setText(this.s2);
			break;
		case "4":
			this.s2 = s2 + 4;
			lventana.setText(this.s2);
			break;
		case "5":
			this.s2 = s2 + 5;
			lventana.setText(this.s2);
			break;
		case "6":
			this.s2 = s2 + 6;
			lventana.setText(this.s2);
			break;
		case "7":
			this.s2 = s2 + 7;
			lventana.setText(this.s2);
			break;
		case "8":
			this.s2 = s2 + 8;
			lventana.setText(this.s2);
			break;
		case "9":
			this.s2 = s2 + 9;
			lventana.setText(this.s2);
			break;
		case "0":
			this.s2 = s2 + 0;
			lventana.setText(this.s2);
			break;
		case "=":
			this.n2 = Integer.parseInt(s2);
			this.s2 = "";
			this.n1 = this.n1 + this.n2;
			this.s1 = String.valueOf(n1);
			lventana.setText(s1);
			break;
		}
	}
}